package abaco.gui.textfield.listener;

import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;

import abaco.gui.textfield.TextField;

public class DocumentListener implements javax.swing.event.DocumentListener {
	protected TextField textField;

	public DocumentListener(TextField textField) {
		this.textField = textField;
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		System.out.println("BoxDocumentListener.changedUpdate()");
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		System.out.println("BoxDocumentListener.removeUpdate()");
		resize();
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		System.out.println("BoxDocumentListener.insertUpdate()");
		resize();
	}

	public void resize() {
		System.out.println("BoxDocumentListener.resize()");

		// change text field size to a comfortable width
		textField.autoColumns();

		// revalidate parent
		if (textField.getParent() != null) {
			JPanel layer = (JPanel) textField.getParent();
			layer.revalidate();
		}
	}
}
